package smallChanges.statements;

import java.util.Comparator;
import java.util.TreeSet;

import smallChanges.Tokenization.CodeRange2D;

/**
 * @author chadd
 *
 * this class contains a set of StatementMappingEntries in an ordered set (TreeSet). The
 * entries are ordered by the left line numbers.  There is also provided a set of methods
 * that allow the user to access the entries in sorted order by the right line numbers.
 */
public class StatementMappingResultSet extends TreeSet<StatementMappingEntry> {

	private static final long serialVersionUID = 1L;
	
	TreeSet<StatementMappingEntry> rightSorted = new TreeSet<StatementMappingEntry> (new Comparator<StatementMappingEntry>(){
		
		public int compare(StatementMappingEntry leftObj, StatementMappingEntry rightObj)
		{
	        int result = 0;
	        
	        CodeRange2D left,right;
	        
	        left = (CodeRange2D) leftObj.getRightStatement();
	        right = (CodeRange2D) rightObj.getRightStatement();
	        
	        result =left.compareTo(right); 
	        if( result == 0 )
	        {
	            left = (CodeRange2D) leftObj.getLeftStatement();
	            right = (CodeRange2D) rightObj.getLeftStatement();
	            result =left.compareTo(right); 
	        }
	        return result;

			/*if ( leftObj.getLeftStatement().getBeginLine() < rightObj.getLeftStatement().getBeginLine() )
			{
				return -1;
			}
			if ( leftObj.getLeftStatement().getBeginLine() > rightObj.getLeftStatement().getBeginLine()  )
			{
				return 1;
			}
			return 0;*/			
		}
	});

	//add
	public boolean add(StatementMappingEntry obj)
	{
		rightSorted.add(obj);
		
		return super.add(obj);
	}
	
	//addAll
	@SuppressWarnings("unchecked")
	public boolean addAll(java.util.Collection obj)
	{
		rightSorted.addAll(obj);
		
		return super.addAll(obj);
	}
		
	
	//clear
	public void clear()
	{
		rightSorted.clear();
		super.clear();
	}
	
	//remove
	public boolean remove(StatementMappingEntry obj)
	{
		rightSorted.remove(obj);
		
		return super.remove(obj);
	}
	
	/* add the following */
	//rightSortedFirst
	public StatementMappingEntry rightSortedFirst()
	{
		return rightSorted.first();
	}
	
	//rightSortedHeadSet
	public java.util.SortedSet<StatementMappingEntry> rightSortedHeadSet(StatementMappingEntry toElement)
	{
		return rightSorted.headSet(toElement);
	}
	
	//rightSortedIterator
	public java.util.Iterator<StatementMappingEntry> rightSortediterator()
	{
		return rightSorted.iterator();
	}

	//rightSortedLast
	public StatementMappingEntry rightSortedLast()
	{
		return rightSorted.last();
	}
	 
	//rightSortedSubSet
	public java.util.SortedSet<StatementMappingEntry> rightSortedSubSet(StatementMappingEntry fromElement, StatementMappingEntry toElement)
	{
		return rightSorted.subSet(fromElement, toElement);
	}
	
	//rightSortedTailSet
	public java.util.SortedSet<StatementMappingEntry> rightSortedTailSet(StatementMappingEntry fromElement)
	{
		return rightSorted.tailSet(fromElement);
	}
	
}
